import 'dart:io';
import 'package:sqflite/sqflite.dart';
///    author : leeo
///    e-mail : jkliao@yeah.net
///    date   : 2020-03-2910:02
///    desc   : 数据库管理
///    1、数据库版本号、数据库名
///    2、创建数据库
///    3、公用的：数据表是否存在
///    4、To Do 数据库升级
///    5、To Do
 class SqfManager {
  static const _VERSION = 1;
  static const _NAME = 'my_sqf.db';

  static Database _database;

  static init() async {
  // Get a location using getDatabasesPath
   var databasesPath = await getDatabasesPath();
   String path = databasesPath + _NAME;
   if (Platform.isIOS) {
    path = databasesPath + "/" +_NAME;
   }
   _database = await openDatabase(path, version: _VERSION, onCreate: (Database db, int ver) async  {
    // When creating the db, create the table
   });
  }

  ///判断表是否存在
  static isTableExits(String tabName) async {
   await getCurrentDb();
   var res = await _database.rawQuery("SELECT * FROM Sqlite_master WHERE TYPE = 'table' and name='$tabName'");
   return res != null && res.length > 0;
  }

  ///获得当前的数据库
  static getCurrentDb() async {
   if (_database == null) {
    await init();
   }
   return _database;
  }

  ///关闭数据
  static close() async {
   await _database?.close();
   _database = null;
  }
 }